package org.study.swing.study02.demo;

import java.sql.*;

public class Connect {
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
    private static String userName = "root";
    private static String password = "root";
    private Connection conn;
    private Statement stmt;

    public Connect() {
        try {
            // 加载数据库驱动
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        // 使用DriverManger获取数据库连接
        return DriverManager.getConnection(url, userName, password);
    }

    public void dispose() {
        try {
            if (conn != null) {
                conn.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
